﻿using AuthCenter.API.Application.Command.Role;
using AuthCenter.Domain.Account;
using AuthCenter.ErrorCode;
using AuthCenter.Intertructrue.Interface;
using MediatR;

namespace AuthCenter.API.Application.Handler.Role
{
    public class DelRoleHandler : IRequestHandler<DelRoleCommand, APIResult<int>>
    {
        private readonly IBaseRepository<RoleModel> roleRepo;

        public DelRoleHandler(IBaseRepository<RoleModel> roleRepo)
        {
            this.roleRepo = roleRepo;
        }
        public Task<APIResult<int>> Handle(DelRoleCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = roleRepo.GetAll().FirstOrDefault(x=>x.Id == request.Id);
            if (res == null)
            {
                result.Message = "找不到此编号";
                result.Code = APIEnum.Fail;
                return Task.FromResult(result);
            }
            res.IsDel = true;
            int r = roleRepo.UpdateAsync(res).Result;
            if (r > 0)
            {
                result.Message = "删除角色信息成功";
                result.Code = APIEnum.Ok;
                result.Data = r;
            }
            else
            {
                result.Message = "删除角色信息失败";
                result.Code = APIEnum.Fail;
                result.Data = r;
            }
            return Task.FromResult(result);
        }
    }
}
